using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using OrbitOne.Elmah.Logic;

namespace OrbitOne.Elmah.Presenter
{
    public class ErrorSearchRow : WebControl
    {
        public OrbitOneElmahErrorFacade Error { get; set; }

        protected override void Render(HtmlTextWriter writer)
        {
            if (Error != null)
            {
                var link = new HyperLink
                               {
                                   NavigateUrl = "../Pages/ErrorDetail.aspx?id=" + Error.Id,
                                   ToolTip = "Detailed view",
                                   CssClass = CssClass
                               };

                var column1 = new HtmlGenericControl("span");
                column1.Attributes.Add("class", "searchcol1");
                column1.InnerHtml = Error.HappeningTime.ToString("g");

                var column2 = new HtmlGenericControl("span");
                column2.Attributes.Add("class", "searchcol2");
                column2.InnerHtml = Error.HostName;

                var column3 = new HtmlGenericControl("span");
                column3.Attributes.Add("class", "searchcol3");
                column3.InnerHtml = Error.ApplicationName;

                var column4 = new HtmlGenericControl("span");
                column4.Attributes.Add("class", "searchcol4");
                column4.InnerHtml = HttpUtility.HtmlEncode(Error.ErrorMessage);

                const string css = "searchimagecol";
                const string imagecss = "imgicon";

                var column5 = new SearchImageColumn
                                  {
                                      ToolTip = Error.UserInfo,
                                      CssClass = css,
                                      FloatDirection = SearchImageColumn.Direction.Right,
                                      ImageCss = imagecss,
                                      ImageSource = Utility.GetUserImageName(Error.UserType)
                                  };
                var column6 = new SearchImageColumn
                                  {
                                      ToolTip = "Error level: " + Error.ErrorLevel,
                                      CssClass = css,
                                      FloatDirection = SearchImageColumn.Direction.Right,
                                      ImageCss = imagecss,
                                      ImageSource = Utility.GetErrorLevelImageName(Error.ErrorLevel)
                                  };
                var column7 = new SearchImageColumn
                                  {
                                      ToolTip = "Error type: " + Error.ErrorType,
                                      CssClass = css,
                                      FloatDirection = SearchImageColumn.Direction.Right,
                                      ImageCss = imagecss,
                                      ImageSource = Utility.GetErrorTypeImageName(Error.ErrorType)
                                  };

                link.Controls.Add(column1);
                link.Controls.Add(column2);
                link.Controls.Add(column3);
                link.Controls.Add(column4);
                link.Controls.Add(column5);
                link.Controls.Add(column6);
                link.Controls.Add(column7);

                link.RenderControl(writer);
            }
        }
    }
}